﻿using System;
using System.Collections.Generic;
using System.Text;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using DevExpress.ExpressApp.DC;
using DevExpress.ExpressApp.SystemModule;
using DevExpress.ExpressApp;
using DevExpress.ExpressApp.ConditionalAppearance;

namespace DomainComponents.XCRM {
    public enum QuoteStatus {
        None = 0,
        Lost = 1,
        Canceled = 2,
        Won = 3
    }

    [DomainComponent]
    public interface IQuoteCustomer : ICustomer {
        [BackReferenceProperty("BackRefCustomer")]
        IList<IQuote> Quotes { get; }
    }

    [DomainComponent]
    public interface IQuoteOpportunity : IOpportunity {
        [BackReferenceProperty("BackRefOpportunity")]
        IList<IQuote> Quotes { get; }
    }

    [DomainComponent]
    [VisibleInReports]
    [ImageName("BO_Quote")]
    [ListViewFilter("Closed Quotes", "[Status] != 'None'", "Closed Quotes", Index = 0)]
    [ListViewFilter("My Open Quotes", "[Owner.Oid] = custom('CurrentUserId') AND [Status] = 'None'", "My Open Quotes", true, Index = 1)]
    [ListViewFilter("Open Quotes", "[Status] = 'None'", "Open Quotes", Index = 2)]
    [XafDisplayName("Quote")]
    public interface IQuote : ISaleBase, IOpportunityTarget {
        QuoteStatus Status { get; set; }

        [VisibleInListView(false)]
        DateTime EffectiveTo { get; set; }

        [VisibleInListView(false)]
        DateTime EffectiveFrom { get; set; }

        [VisibleInListView(false)]
        DateTime DueBy { get; set; }

        [VisibleInListView(false)]
        [System.ComponentModel.Browsable(false)]
        DateTime ClosedOn { get; set; }

        [BackReferenceProperty("SourceQuote")]
        IList<IOrder> Orders { get; }

        [Action(PredefinedCategory.View, Caption = "Close Quote...", AutoCommit = true,
          SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void Close(CloseQuoteParameters parameters);
        
        [Action(PredefinedCategory.View, Caption = "Create Order...", AutoCommit = true,
          SelectionDependencyType = MethodActionSelectionDependencyType.RequireSingleObject)]
        void CreateOrderFromQuote(CreateOrderFromQuoteParameters parameters);

        [CreateInstance]
        IOrder CreateOrder();
        
        [System.ComponentModel.Browsable(false)]
        [MemberDesignTimeVisibility(false)]
        IQuoteCustomer BackRefCustomer { get; set; }//TODO: duplicated with IInvoce, IOpportunity, IOrder, IQuote
        
        [System.ComponentModel.Browsable(false)]
        IQuoteOpportunity BackRefOpportunity { get; set; }//TODO
    }

    [DomainLogic(typeof(IQuote))]
    public class QuoteLogic {
        public static void AfterChange_BackRefCustomer(IQuote saleBase) {
            saleBase.PotentialCustomer = saleBase.BackRefCustomer;
        }
        public static void AfterChange_PotentialCustomer(IQuote saleBase) {
            saleBase.BackRefCustomer = (IQuoteCustomer)saleBase.PotentialCustomer;
        }
        public static void AfterChange_BackRefOpportunity(IQuote saleBase) {
            saleBase.SourceOpportunity = saleBase.BackRefOpportunity;
        }
        public static void AfterChange_SourceOpportunity(IQuote saleBase) {
            saleBase.BackRefOpportunity = (IQuoteOpportunity)saleBase.SourceOpportunity;
        }
    }
}
